package com.ruoyi.web.controller.system;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Packages;
import com.ruoyi.system.service.IPackageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 套餐类型Controller
 *
 * @author ruoyi
 * @date 2021-07-09
 */
@RestController
@RequestMapping("/system/packages")
public class PackageController extends BaseController {
    @Autowired
    private IPackageService packageService;

    /**
     * 查询套餐类型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:package:list')")
    @GetMapping("/list")
    public TableDataInfo list(Packages packages) {
        startPage();
        List<Packages> list = packageService.selectPackageList(packages);
        return getDataTable(list);
    }

    /**
     * 导出套餐类型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:package:export')")
    @Log(title = "套餐类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Packages packages) {
        List<Packages> list = packageService.selectPackageList(packages);
        ExcelUtil<Packages> util = new ExcelUtil<Packages>(Packages.class);
        return util.exportExcel(list, "package");
    }

    /**
     * 获取套餐类型详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:package:query')")
    @GetMapping(value = "/{pId}")
    public AjaxResult getInfo(@PathVariable("pId") Long pId) {
        return AjaxResult.success(packageService.selectPackageById(pId));
    }

    /**
     * 新增套餐类型
     */
//    @PreAuthorize("@ss.hasPermi('system:package:add')")
    @Log(title = "套餐类型", businessType = BusinessType.INSERT)
    @PostMapping
    @CrossOrigin
    public AjaxResult add(Packages packages, @RequestParam(value = "file") MultipartFile file) {
        packageService.insertPackage(packages);
        try {
            String profile = RuoYiConfig.getProfile();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile + "//pId//" + packages.getpId());
            //具体文件
            Path path = Paths.get(profile + "//pId//" + packages.getpId() + "//" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                return AjaxResult.error("文件上传失败,文件存在");
            } else {
                Files.write(path, bytes);//创建文件
                packages.setpUrl("profile/pId/" + packages.getpId() + "/" + file.getOriginalFilename());
            }
        } catch (Exception e) {

        }
        return toAjax(packageService.updatePackage(packages));
    }

    /**
     * 修改套餐类型
     */
//    @PreAuthorize("@ss.hasPermi('system:package:edit')")
    @Log(title = "套餐类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit( Packages packages,MultipartFile file,Integer fileState) {
        if (fileState==1) {
            try {

                String profile = RuoYiConfig.getProfile();
                byte[] bytes = file.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//pId//" + packages.getpId());
                //具体文件
                Path path = Paths.get(profile + "//pId//" + packages.getpId() + "//" + file.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    return AjaxResult.error("文件上传失败,文件存在");
                } else {
                    Files.write(path, bytes);//创建文件
                    packages.setpUrl("profile/pId/" + packages.getpId() + "/" + file.getOriginalFilename());
                }
            } catch (Exception e) {

            }
        }
        return toAjax(packageService.updatePackage(packages));
    }

    /**
     * 删除套餐类型
     */
//    @PreAuthorize("@ss.hasPermi('system:package:remove')")
    @Log(title = "套餐类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pIds}")
    public AjaxResult remove(@PathVariable Long[] pIds) {
        return toAjax(packageService.deletePackageByIds(pIds));
    }
}
